<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="分组" prop="group_id">
        <el-select v-model="form.group_id" placeholder="分组">
          <el-option
            v-for="item in group"
            :key="item.id"
            :label="item.group_name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间号" prop="home_name">
        <el-input v-model="form.home_name" placeholder="请输入房间号" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { editRoom } from '@/api/room'
  import { getgroup } from '@/api/group'
  export default {
    name: 'EditRoom',
    data() {
      return {
        form: {
          home_name: '',
          group_id: '',
        },
        rules: {
          group_id: [
            { required: true, trigger: 'blur', message: '请选择分组' },
          ],
          room_no: [
            { required: true, trigger: 'blur', message: '请输入房间号' },
          ],
        },
        title: '编辑整套',
        dialogFormVisible: false,
        group: [],
      }
    },
    methods: {
      async showEdit(row) {
        this.getGroup()
        this.form.home_name = row.home_name
        this.form.home_id = row.home_id
        this.form.community_id = row.community_id
        this.form.group_id = row.group_id
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { data } = await editRoom(this.form)
            if (data.is_success) {
              this.$baseMessage(
                '修改成功',
                'success',
                'vab-hey-message-success'
              )
              this.$emit('fetch-data')
              this.close()
            }
          }
        })
      },
      async getGroup() {
        const { data } = await getgroup({})
        this.group = data
      },
    },
  }
</script>
